import { stringToDate } from "@/generic/Utils";
import { DEFAULT_USER_PASSWORD } from "@/generic/ConfigParams";
import { Icon, Input, Button, Transfer } from 'antd';
import { MyTransfer } from "../components/CustomHTML";
import { queryUsers } from "@/services/user";
import { queryGroupUsers, updateGroupUsers} from "../service";

const { TextArea } = Input;

export const TABLE_COLS = [
    {
        dataIndex: 'key', text: "序号", width: '', isHidden: true,
    },
    {
        dataIndex: 'name', text: "组名", width: '',
        canClick: { type: 'manager/updateCurrentPage', payload: { mark: 'detail' } },
    },
    {
        dataIndex: 'desc', text: "描述", width: '',
    },
    {
        dataIndex: 'isSys', text: "系统创建", width: '', isHidden: true,
        render: (isSys, _) => isSys
            ? <Icon type="check-circle" style={{ color: '#52c41a' }} />
            : <Icon type="close-circle" style={{ color: 'gray' }} />,
    },
]

export const HEADER_OPTS = {
    left: [
        {
            isGroup: false,
            text: '添加',
            className: 'headerBtns',
            args: { icon: 'plus', type: 'primary', },
            action: { type: 'manager/updateModalVisible', payload: true },
        },
        {
            isGroup: false,
            text: '删除',
            className: 'headerBtns',
            args: { icon: 'delete', type: 'danger', },
            isHidden: true,
            confirm: {
                name: '组',
                action: { type: 'manager/methodAdapter', method: 'delete', payload: {} },
            },
            msgTip: '删除',
        },
    ],
    right: {
        onSearch: () => { },
    },
}

export const MODAL_CFGS = {
    // okText: '',  // 设置submit按钮文本
    // cancelText: '',  // 设置cance按钮文本
    title: '添加组',
    defaultLabelCol: { span: 5 },
    defaultWrapperCol: { span: 15 },
    okAction: { type: 'manager/methodAdapter', method: 'add', payload: { } },
    onCancel: (dispatch) => dispatch({ type: 'manager/updateModalVisible', payload: false }),
    formItems: [
        {
            label: '组名', name: 'name',
            options: {
                rules: [{ required: true, message: '请输入至少两个字符的名称！', min: 2, }],
            },
            html: <Input placeholder="请输入组的名名称" />,
        },
        {
            label: '描述', name: 'desc',
            html: <Input placeholder="请输入组的描述" />,
        },
    ],
}

//  #### DETAIL_ARGS ####
const leave = { type: 'manager/updateCurrentPage', payload: null }
const TFparams = [
    { newKey: 'key', resKey: 'id'},  // 必须
    { newKey: 'title', resKey: 'username' },  //必须
    { newKey: 'description', resKey: 'email' },
];
export const DETAIL_ARGS = {
    detailTitle: '组详情',
    updateTitle: '组修改',
    leave: leave,
    defaultLabelCol: { span: 5 },
    defaultWrapperCol: { span: 15 },
    defaultColSpan: { xs: 24, md: 12, lg: 12 },
    content: {
        group: [
            {
                label: '组名', index: 'name', canUpdate: true,
            },
            {
                label: '描述', index: 'desc', canUpdate: true,
                elem: <TextArea rows={3}/>
            },
            { label: '系统创建', index: 'isSys' },
            {  // isMine：代表是自己自定义添加的字段 | // 'extra_'代表执行特定处理方法（handMethod）
                label: '组员', index: 'extra_users', isRow: { xs: 24, md: 20, lg: 20 }, 
                labelCol: { span: 3 }, wrapperCol: { span: 21 }, canUpdate: true,
                isMine: { defaultIndex: 'name', extra_method: queryGroupUsers, targetKey: 'username' },
                elem: <MyTransfer 
                        reqMethod={queryUsers} 
                        initMethod={queryGroupUsers} 
                        extraMethod={updateGroupUsers} 
                        params={TFparams}
                    />
            },
        ]
    },
    footer: [
        {
            elem: Icon,
            style: { float: 'left', height: '32px', lineHeight: '32px' },
            args: {
                type: 'arrow-left',
            },
            onClick: (dispatch) => { dispatch(leave) },
            // html: <xxx />  // 复杂的直接返回组件
        },
        {
            mark: 'detail',
            elem: Button,
            style: { float: 'right', marginRight: '8px' },
            args: {
                // icon: 'edit',
                type: 'danger',
            },
            text: '删除',
            onClick: (dispatch) => { }
        },
        {
            mark: 'detail',
            elem: Button,
            style: { float: 'right', marginRight: '8px' },
            args: {
                // icon: 'edit',
                type: 'primary',
            },
            text: '修改',
            onClick: (dispatch, record) => {
                dispatch({ type: 'manager/updateCurrentPage', payload: { mark: 'update', record } })
            }
        },
        {
            mark: 'update',
            elem: Button,
            style: { float: 'right', marginRight: '8px' },
            text: '取消',
            onClick: (dispatch) => { }
        },
        {
            mark: 'update',
            elem: Button,
            style: { float: 'right', marginRight: '8px' },
            args: {
                // icon: 'edit',
                type: 'primary',
                // htmlType="submit",
            },
            text: '保存',
            isValidate: true,
            onClick: (dispatch, record, updateFields) => {
                return dispatch({ type: 'manager/methodAdapter', method: 'update', payload: { value: updateFields, rows: [record] } })
            }
        },
    ],
}